package com.survey.wxapp.controller;

import com.survey.common.annotation.Anonymous;
import com.survey.common.config.RuoYiConfig;
import com.survey.common.core.controller.BaseController;
import com.survey.common.core.domain.AjaxResult;
import com.survey.common.utils.StringUtils;
import com.survey.common.utils.file.FileUploadUtils;
import com.survey.common.utils.file.FileUtils;
import com.survey.common.utils.file.MimeTypeUtils;
import com.survey.framework.config.ServerConfig;
import com.survey.system.domain.SurIssue;
import com.survey.system.service.ISurIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Anonymous
@RestController
@RequestMapping("/wx/issue")
public class WxIssueController extends BaseController {

    @Autowired
    private ISurIssueService surIssueService;

    @Autowired
    private ServerConfig serverConfig;

    private static final String FILE_DELIMETER = ",";

    /**
     * 用户查询问题
     * @param surIssue
     * @return
     */
    @PostMapping("/list")
    public AjaxResult select(@RequestBody SurIssue surIssue) {
        if (surIssue.getUserId() == null){
            return success();
        }
        return success(surIssueService.selectSurIssueList(surIssue));
    }

    /**
     * 用户查询问题详情
     * @param surIssueId
     * @return
     */
    @PostMapping("/detail")
    public AjaxResult detail(Long surIssueId) {
        return success(surIssueService.selectSurIssueByIssueId(surIssueId));
    }


    /**
     * 用户新增问题
     * @param surIssue
     * @return
     */
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody SurIssue surIssue) {
        return success(surIssueService.insertSurIssue(surIssue));
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

}
